/**
 * 
 */
package util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import objects.CircularItem;

import abstracts.AItem;

import org.ccil.cowan.tagsoup.AttributesImpl;
import org.ccil.cowan.tagsoup.HTMLScanner;
import org.ccil.cowan.tagsoup.Parser;
import org.ccil.cowan.tagsoup.ScanHandler;
import org.ccil.cowan.tagsoup.Scanner;
import org.hamcrest.core.IsNull;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * @author Christopher Fingar
 * Given URL retrieves HTML for that URL and 
 * parses the page looking for CircularItem data. 
 */
public class ParseFredMeyerItemHtml implements ScanHandler {

	private HttpCookie _parserCookie;
	private URL _parserUrl;
	private HttpURLConnection _parserUrlConnection;
	private Collection<String> _parserHtml;
	//private AItem _fredMeyerItem;
	
	public ParseFredMeyerItemHtml() {
		// TODO Auto-generated constructor stub
		_parserCookie = null;
		_parserUrl = null;
		_parserUrlConnection = null;
		//set_parserHtml(null);
		TagData = new StringBuilder();
	}
	
	public ParseFredMeyerItemHtml(URL parserURL)
	{
		_parserUrl = parserURL;
		
		
		
		try {
			_parserUrlConnection.setInstanceFollowRedirects(false);
			_parserUrlConnection = (HttpURLConnection) (_parserUrl.openConnection());
			_parserUrlConnection.setInstanceFollowRedirects(false);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

/*	@Override public void startElement(String uri,String localName, String qName,Attributes attributes)throws SAXException
	{
		/*System.out.println("+++++++++++");
		System.out.println(uri);
		System.out.println(localName);
		System.out.println(qName);		
		System.out.println(attributes.getLength());
		for (int a = 0; a< attributes.getLength(); a++)
		{	
			System.out.println(" -------");
			System.out.println(attributes.getValue(a));
			System.out.println(" -------");
		}
		
		System.out.println("+++++++++++");*/
	//}
	/*@Override public void characters(char[] ch,int start, int length) throws SAXException
	{
		System.out.println("+++++++++++");
		//System.out.println(ch);
		String newCh = String.copyValueOf(ch);
		System.out.println(newCh.substring(start, start+length));
		
		System.out.println(start);
		System.out.println(length);		
		
		
		
		System.out.println("+++++++++++");
		
	}*/
	public AItem ParseUrl() throws IOException, SAXException
	{

		AItem result = new CircularItem();
		
		//getHtmlFromStream();
		
		Parser tParse = new Parser();
		//ContentHandler handler = new DefaultHandler();
		AttributesImpl atts = new AttributesImpl();
		atts.addAttribute("", "", "", "class","breakoutItemName"); 
		_parserUrlConnection.setInstanceFollowRedirects(false);
		//_parserUrlConnection.setRequestProperty("Cookie", "drpstoreid=13; shoppingFlag=shoppingFlag; BIGipServerI2O_pool_A=1782002698.20480.0000; showflash=true; JSESSIONID=B1446435AFA5AFE6EA4E79F191BAC9BD");
		//System.out.println(_parserUrlConnection.getRequestProperty("Cookie"));
		//this.startElement("", "", "div", atts);
		Scanner scna = new HTMLScanner();
//BIGipServerI2O_pool_A=1714893834.20480.0000;JSESSIONID=9CE9266A698501E38F88BD0DC5954F23;		
		if(_parserUrlConnection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP)
		{
			
			
			try {
				System.out.println("getcookie");
				return CreateParserCookie();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		System.out.println(_parserUrlConnection.getInstanceFollowRedirects());
		System.out.println(_parserUrlConnection.getRequestMethod());
		System.out.println(_parserUrlConnection.getRequestProperty("Accept"));
		Reader red = new InputStreamReader(_parserUrlConnection.getInputStream());
		ScanHandler sh = this;
		scna.scan(red, sh);
		tParse.setContentHandler(tParse);
		InputSource input = new InputSource(_parserUrlConnection.getInputStream()); 
		tParse.parse(input);
		
		
		
		return result;
		
		
	}
	
	private AItem CreateParserCookie() throws IOException, SAXException, InterruptedException {
	// 
		Thread.currentThread().sleep(3000);
		System.out.println("currentCookie start: " + _parserUrlConnection.getRequestProperty("Cookie"));
		//if(_parserUrlConnection.getRequestProperty("Cookie") == null || _parserUrlConnection.getRequestProperty("Cookie").isEmpty()){
		Map<String, List<String>> headers = _parserUrlConnection.getHeaderFields();
		List<String> values = headers.get("Set-Cookie");
		if( values != null ){
			System.out.println("create cookie");
			String cookieValue = null;
			for (Iterator iter = values.iterator(); iter.hasNext(); ) {
			     String v = (String) iter.next();
			     if (cookieValue == null)
			         cookieValue = v;
			     else
			         cookieValue = cookieValue + ";" + v;
				} 
			cookieValue = cookieValue + ";shoppingFlag=shoppingFlag;showflash=false";
			cookieValue = cookieValue.replaceAll(" path=/;", "");
			cookieValue = cookieValue.replaceAll(" Path=/;", "");
			_parserUrlConnection.disconnect();
			
			URL backup = new URL(_parserUrl.toExternalForm());
			//set_parserUrl(new URL("http://fredmeyer.inserts2online.com/customer_Frame.jsp?divID=701&drpStoreID=00013"));
			//set_parserUrl(new URL("http://fredmeyer.inserts2online.com/availableAds.jsp?drpStoreID=13"));
			set_parserUrl(new URL("http://fredmeyer.inserts2online.com/loadServletXml.jsp?adId=31467&showFlash=false"));
			
			
			_parserUrlConnection.setInstanceFollowRedirects(false);
			
			
	//		_parserCookie.setValue(cookieValue);
			//_parserCookie.
			cookieValue = Pattern.compile("Path.*?;",Pattern.CASE_INSENSITIVE).matcher(cookieValue).replaceAll("");
			
			_parserUrlConnection.setRequestMethod("GET");
			_parserUrlConnection.setRequestProperty("Accept", "*/*");
			_parserUrlConnection.setRequestProperty("Accept-Language","en-us;");
			_parserUrlConnection.setRequestProperty("UA-CPU","x86");
			_parserUrlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
			_parserUrlConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30618)");
			_parserUrlConnection.setRequestProperty("Connection",  "Keep-Alive");
			_parserUrlConnection.setRequestProperty("Host", "fredmeyer.inserts2online.com");
			_parserUrlConnection.setRequestProperty("Request Version", "HTTP/1.11");
			_parserUrlConnection.setRequestProperty("Keep-Alive", "115");
			_parserUrlConnection.setRequestProperty("Cookie", cookieValue);
			int rc = _parserUrlConnection.getResponseCode();
			if(rc == HttpURLConnection.HTTP_MOVED_TEMP)
			{
				System.out.println("nogood still");
				System.out.println("ASDF" + _parserUrlConnection.getHeaderField("Location"));
				//return null;
			}
			else
			{
				System.out.println(_parserUrlConnection.getResponseMessage());
				System.out.println("better");
				System.out.println(rc);
			}
			_parserUrlConnection.disconnect();			
			
			
			set_parserUrl(backup);
			_parserUrlConnection.setInstanceFollowRedirects(false);
			_parserUrlConnection.setRequestMethod("GET");
			_parserUrlConnection.setRequestProperty("Accept", "*/*");
			_parserUrlConnection.setRequestProperty("Accept-Language","en-us;");
			_parserUrlConnection.setRequestProperty("UA-CPU","x86");
			_parserUrlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
			_parserUrlConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30618)");
			_parserUrlConnection.setRequestProperty("Connection",  "Keep-Alive");
			_parserUrlConnection.setRequestProperty("Host", "fredmeyer.inserts2online.com");
			_parserUrlConnection.setRequestProperty("Cookie", cookieValue);
			System.out.println(_parserUrlConnection.getResponseMessage());
			Thread.currentThread().sleep(3000);
		}
	//}
		System.out.println("currentCookie end: " + _parserUrlConnection.getRequestProperty("Host"));
		return ParseUrl();
	}

	protected void getHtmlFromStream() throws IOException
	{
		BufferedReader in = new BufferedReader(
        new InputStreamReader(
                _parserUrlConnection.getInputStream()));
		
		String inputLine;

		while ((inputLine = in.readLine()) != null) 
			_parserHtml.add(inputLine);
		in.close();
	}
	
	/**
	 * @param _parserCookie the _parserCookie to set
	 */
	public void set_parserCookie(HttpCookie _parserCookie) {
		this._parserCookie = _parserCookie;
	}
	/**
	 * @return the _parserCookie
	 */
	public HttpCookie get_parserCookie() {
		return _parserCookie;
	}
	/**
	 * @param _parserUrlConnection the _parserUrlConnection to set
	 */
	public void set_parserUrlConnection(HttpURLConnection _parserUrlConnection) {
		this._parserUrlConnection = _parserUrlConnection;
	}
	/**
	 * @return the _parserUrlConnection
	 */
	public URLConnection get_parserUrlConnection() {
		return _parserUrlConnection;
	}
	/**
	 * @param _fredMeyerItem the _fredMeyerItem to set
	 */




	/**
	 * @param _parserUrl the _parserUrl to set
	 * @throws IOException 
	 */
	public void set_parserUrl(URL ParserUrl) throws IOException {
		this._parserUrl = ParserUrl;
		_parserUrlConnection = (HttpURLConnection) _parserUrl.openConnection();
	}




	/**
	 * @return the _parserUrl
	 */
	public URL get_parserUrl() {
		return _parserUrl;
	}

	@Override
	public void adup(char[] buff, int offset, int length) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void aname(char[] buff, int offset, int length) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void aval(char[] buff, int offset, int length) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void cdsect(char[] buff, int offset, int length) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void decl(char[] buff, int offset, int length) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void entity(char[] buff, int offset, int length) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void eof(char[] buff, int offset, int length) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void etag(char[] buff, int offset, int length) throws SAXException {
		// TODO Auto-generated method stub
		System.out.println(tagName);
		System.out.println("+++++TagData++++++");
		//System.out.println(ch);;
		System.out.println(TagData);
		TagData.delete(0,TagData.length());
		tagName = "";
	}
	private String tagName;
	private StringBuilder TagData;
	@Override
	public void gi(char[] ch, int start, int length) throws SAXException {
		// TODO Auto-generated method stub
		String newCh = String.copyValueOf(ch);		
		tagName = "Tag:" + newCh.substring(start, start+length);
		
	}

	@Override
	public void pcdata(char[] ch, int start, int length) throws SAXException {
		
		System.out.println(tagName);
		System.out.println("+++++TagData++++++");
		//System.out.println(ch);
		String newCh = String.copyValueOf(ch);
		if(length>0)
		{
			
			if(TagData.equals(null))
			{
				System.out.println("still NULL2");
			}
			TagData.append(newCh.substring(start, start+length));
		}		
		
	}

	@Override
	public void pi(char[] buff, int offset, int length) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pitarget(char[] buff, int offset, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void stagc(char[] buff, int offset, int length) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void stage(char[] buff, int offset, int length) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void cmnt(char[] buff, int offset, int length) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getEntity() {
		// TODO Auto-generated method stub
		return 0;
	}


	/**
	 * @param _parserHtml the _parserHtml to set
	 
	public void set_parserHtml(String _parserHtml) {
		this._parserHtml = _parserHtml;
	}

	/**
	 * @return the _parserHtml
	 
	public String get_parserHtml() {
		return _parserHtml;
	}*/
	
}
